package com.yuelao.thread.demo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 线程屏障
 * Created by fengliye on 2017/10/7.
 */
public class CyclicBarrierTest {
    // 线程到达屏障后，优先执行new  ThreadTest()
    static CyclicBarrier c = new CyclicBarrier(2, new ThreadTest());

    public static void main(String[] args) {
        new Thread(() ->{
            try {
                c.await();
               // Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("1");
        }).start();
        try {
            c.await();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("2");

    }
    static class ThreadTest implements Runnable{
        @Override
        public void run() {
            System.out.println("3");
        }
    }
}
